package com.itheima.info.manager.controller;

import com.itheima.info.manager.domain.Student;
import com.itheima.info.manager.service.StudentService;

import java.util.Scanner;

public class StudentController {

    private StudentService studentService = new StudentService();
    private Scanner scanner = new Scanner(System.in);


    public void start() {

        stulo:
        while (true) {

            System.out.println("--------欢迎来到 <学生> 管理系统--------");
            System.out.println("请输入您的选择: 1.添加学生  2.删除学生  3.修改学生  4.查看学生  5.退出");

            String s = scanner.next();

            switch (s) {

                case "1":

                    addStudent();
                    break;

                case "2":

                    deleteStudent();
                    break;

                case "3":

                    updateStudent();
                    break;

                case "4":

                    getAllStudent();
                    break;

                case "5":

                    System.out.println("感谢使用学生管理系统");
                    break stulo;

                default:

                    System.out.println("输入有误，请重新输入！");

            }
        }
    }

    public void updateStudent() {
        String updateId = inputStudentId();
        Student newStu = inputStudentInfo(updateId);
        studentService.updateStudent(updateId, newStu);

        System.out.println("修改成功!");
    }

    private void deleteStudent() {
        String stuId = inputStudentId();
        studentService.deleteStudentById(stuId);
        System.out.println("删除成功!");
    }

    public String inputStudentId() {
        String id;
        while (true) {
            System.out.println("请输入学生id:");
            id = scanner.next();
            boolean exists = studentService.isExist(id);
            if (!exists) {
                System.out.println("您输入的id不存在, 请重新输入:");
            } else {
                break;
            }
        }
        return id;
    }

    public void getAllStudent() {
        Student[] stus = studentService.findAllStudent();

        if (stus == null) {
            System.out.println("暂无学生信息！");
            return;
        }

        System.out.println("学号\t\t姓名\t\t年龄\t\t生日");
        for (int i = 0; i < stus.length; i++) {
            Student s = stus[i];
            if (s != null) {
                System.out.println(s.getId() + "\t\t" + s.getName() + "\t\t" + s.getAge() + "\t\t" + s.getBirthday());
            }
        }
    }

    public void addStudent() {

        String stuId;

        while (true) {

            System.out.println("请输入学生学号：");
            stuId = scanner.next();
            boolean flag = studentService.isExist(stuId);

            if (flag) {
                System.out.println("输入的学号已存在，请重新输入！");
            } else {
                break;
            }

        }

        Student student = inputStudentInfo(stuId);

        boolean result = studentService.addStudent(student);

        if (result) {
            System.out.println("添加成功！");
        } else {
            System.out.println("添加失败！");
        }
    }

    public Student inputStudentInfo(String stuId) {

        System.out.println("请输入学生姓名：");
        String stuName = scanner.next();

        System.out.println("请输入学生年龄：");
        String stuAge = scanner.next();

        System.out.println("请输入学生生日：");
        String stuBirthday = scanner.next();

        Student student = new Student();

        student.setId(stuId);
        student.setName(stuName);
        student.setAge(stuAge);
        student.setBirthday(stuBirthday);
        return student;
    }
}
